package com.pospi.xstore.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;

import com.pospi.xstore.R;
import com.pospi.xstore.ui.MainActivity;
import com.pospi.xstore.ui.caigou.CgddActivity;
import com.pospi.xstore.ui.caigou.CgshListActivity;
import com.pospi.xstore.ui.caigou.ReceiveBillListActivity;
import com.pospi.xstore.ui.caigou.RefundBillListActivity;
import com.pospi.xstore.ui.cash_ui.PointActivity;
import com.pospi.xstore.ui.cash_ui.ReceiveActivity;
import com.pospi.xstore.ui.count.CountActivity;
import com.pospi.xstore.ui.cash_ui.FastCheckActivity;
import com.pospi.xstore.ui.cash_ui.PointSelectActivity;
import com.pospi.xstore.ui.cash_ui.ReceiveGoodsActivity;
import com.pospi.xstore.ui.cash_ui.RefundActivity;
import com.pospi.xstore.ui.cash_ui.RestOrderActivity;
import com.pospi.xstore.ui.cash_ui.ScanReceiptActivity;
import com.pospi.xstore.ui.cash_ui.TableActivity;
import com.pospi.xstore.ui.count.CountsActivity;
import com.pospi.xstore.ui.vip_ui.VIPFavourActivity;
import com.pospi.xstore.ui.vip_ui.VipActivity;
import com.pospi.xstore.ui.vip_ui.VipSalesSearchActivity;
import com.pospi.xstore.ui.xs.OutBillListActivity;
import com.pospi.xstore.ui.xs.SaleListActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CashierFragment extends BaseFragment implements AdapterView.OnItemClickListener {
//    @Bind(R.id.ll_scan_receipt)
//    LinearLayout llScanReceipt;
////    @Bind(R.id.ll_poi_receipt)
////    LinearLayout llPoiReceipt;
//    @Bind(R.id.ll_quick_receipt)
//    LinearLayout llQuickReceipt;
//    @Bind(R.id.ll_refund)
//    LinearLayout llRefund;
//    @Bind(R.id.ll_rest_order)
//    LinearLayout llRestOrder;
//    @Bind(R.id.ll_shift_change)
//    LinearLayout llShiftChange;
//    @Bind(R.id.ll_open_shop)
//    LinearLayout llOpenShop;
//    @Bind(R.id.ll_daily)
//    LinearLayout llDaily;
    @Bind(R.id.iv_menu)
    ImageView ivMenu;
    @Bind(R.id.ll_cahier)
    FrameLayout llCahier;
    @Bind(R.id.gridview_main)
    GridView gridview_main;

    private SimpleAdapter adapter;
    private List<Map<String, Object>> dataList;

    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cashier, container, false);
        String[] from={"img","text"};
        int[] to={R.id.img,R.id.text};
        ButterKnife.bind(this, view);
        context = getActivity().getApplication();
        initData();
        adapter=new SimpleAdapter(context, dataList, R.layout.grid_item, from, to);
        gridview_main.setAdapter(adapter);
        gridview_main.setOnItemClickListener(this);
        return view;
    }

    private void initData() {
        //图标
        int icno[] = {R.drawable.icon_sm,R.drawable.icon_th,
                R.drawable.icon_hy_m,R.drawable.icon_gd};
        //图标下的文字
        String name[]={"扫码收款","交易记录",
                "会员","会员消费"};
        dataList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <icno.length; i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("img", icno[i]);
            map.put("text",name[i]);
            dataList.add(map);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.iv_menu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_menu:

                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity.openLeftDrawer();

//                LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                View contentView = mLayoutInflater.inflate(R.layout.main_pop_left, null);
//                // R.layout.pop为PopupWindow 的布局文件
//                PopupWindow pop = new PopupWindow(contentView, TurnSize.dip2px(context, 250), TurnSize.dip2px(context, 100));
//                pop.setBackgroundDrawable(new BitmapDrawable());
//                // 指定 PopupWindow 的背景
//                pop.setFocusable(true);
//                pop.showAsDropDown(llCahier);
//                TextView storeNo = (TextView) contentView.findViewById(R.id.store_no);
//                TextView nameIdTime = (TextView) contentView.findViewById(R.id.name_id_time);
//
//                String storeName = context.getSharedPreferences("StoreMessage", Context.MODE_PRIVATE).getString("Name", "");
//                String value = context.getSharedPreferences("Token", Context.MODE_PRIVATE).getString("value", "");
//                String[] names = value.split("\\,");
//                String deviceNo = names[2];//得到token的值
//                int which = context.getSharedPreferences("islogin", Context.MODE_PRIVATE).getInt("which", 0);
//                List<CashierMsgDto> cashierMsgDtos = new PareseJson().parese(context.getSharedPreferences("cashierMsgDtos", Context.MODE_PRIVATE).getString("cashierMsgDtos", ""));
//                String name = cashierMsgDtos.get(which).getName();
//                String id = cashierMsgDtos.get(which).getNumber();
//                storeNo.setText(storeName + "-" + deviceNo);
//                nameIdTime.setText(name + "-" + id + "-" + GetData.getYYMMDDshow());

                break;
//            case R.id.ll_scan_receipt:
//                startActivity(ScanReceiptActivity.class);
//                break;
////            case R.id.ll_poi_receipt:
////                startActivity(PointSelectActivity.class);
////                break;
//            case R.id.ll_quick_receipt:
//                startActivity(FastCheckActivity.class);
//                break;
//            case R.id.ll_refund:
//                startActivity(RefundActivity.class);
//                break;
//            case R.id.ll_rest_order:
//                startActivity(RestOrderActivity.class);
//                break;
//            case R.id.ll_shift_change:
//                startActivity(CountsActivity.class);
//                break;
//            case R.id.ll_open_shop:
////                startActivity(TableActivity.class);
//                //采购订单
//                startActivity(CgshListActivity.class);
//                break;
//            case R.id.ll_daily:
//                startActivity(ReceiveGoodsActivity.class);
//                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (dataList.get(i).get("text").toString()) {
            case "扫码收款":
                startActivity(ScanReceiptActivity.class);
                break;
            case "挂单":
                startActivity(RestOrderActivity.class);
                break;
            case "交易记录":
                startActivity(RefundActivity.class);
                break;
            case "会员":
                startActivity(VipActivity.class);
                break;
            case "会员消费":
                startActivity(VipSalesSearchActivity.class);
                break;
        }
    }
}
